home *** CD-ROM | disk | FTP | other *** search
/ World of Education / World of Education.iso / world_p / pcshx10b.zip / PCSHX10B.EXE / GNUFGREP.EXE / GREPDOCS.EXE / GLOB.C < prev    next >
C/C++ Source or Header  |  1990-08-31  |  20KB  |  839 lines

  1. /* File-name wildcard pattern matching for GNU.
  2.    Copyright (C) 1985, 1988, 1989, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* MODIFIED FOR MSDOS BY BARRY SCHWARTZ, AUGUST 1990 */
  19.  
  20. /* To whomever it may concern: I have never seen the code which most
  21.    Unix programs use to perform this function.  I wrote this from scratch
  22.    based on specifications for the pattern matching.  --RMS.  */
  23.  
  24. #include <sys/types.h>
  25.  
  26. #ifdef MSDOS
  27. #include "msd_dir.h"
  28. #include <stddef.h>
  29. #include <ctype.h>
  30. #include <errno.h>
  31. #define D_NAMLEN(d) ((d)->d_namlen)
  32. #ifndef USG
  33. #define USG
  34. #endif
  35. #ifndef STDC_HEADERS
  36. #define STDC_HEADERS
  37. #endif
  38. #else /* not MSDOS */
  39. #if defined(USGr3) || defined(DIRENT) || defined(__GNU_LIBRARY__)
  40. #include <dirent.h>
  41. #define direct dirent
  42. #define D_NAMLEN(d) strlen((d)->d_name)
  43. #else /* not USGr3 or DIRENT or __GNU_LIBRARY__ */
  44. #define D_NAMLEN(d) ((d)->d_namlen)
  45. #ifdef USG
  46. #ifdef SYSNDIR
  47. #include <sys/ndir.h>
  48. #else
  49. #include "ndir.h"        /* Get ndir.h from the Emacs distribution.  */
  50. #endif /* not SYSNDIR */
  51. #else /* not USG */
  52. #include <sys/dir.h>
  53. #endif /* USG */
  54. #endif /* USGr3 */
  55. #endif /* MSDOS */
  56.  
  57. #if    defined(STDC_HEADERS) || defined(__GNU_LIBRARY__)
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #define bcopy(s, d, n) memcpy ((d), (s), (n))
  61. #define index strchr
  62. #define rindex strrchr
  63. #else
  64.  
  65. #ifdef USG
  66. #include <string.h>
  67. #include <memory.h>
  68. #define bcopy(s, d, n) memcpy ((d), (s), (n))
  69. #define index strchr
  70. #define rindex strrchr
  71. #else /* not USG */
  72. #include <strings.h>
  73. extern void bcopy ();
  74. #endif /* not USG */
  75.  
  76. extern char *malloc ();
  77. extern char *realloc ();
  78. extern void free ();
  79.  
  80. #ifndef NULL
  81. #define NULL 0
  82. #endif
  83. #endif    /* Not STDC_HEADERS or __GNU_LIBRARY__.  */
  84.  
  85. #ifdef __GNUC__
  86. #define alloca __builtin_alloca
  87. #else /* Not GCC.  */
  88. #ifdef sparc
  89. #include <alloca.h>
  90. #else /* Not sparc.  */
  91. #ifdef MSDOS
  92. #include <malloc.h>
  93. #else /* Not MSDOS.  */
  94. extern char *alloca ();
  95. #endif /* MSDOS.  */
  96. #endif /* sparc.  */
  97. #endif /* GCC.  */
  98.  
  99. /* Nonzero if '*' and '?' do not match an initial '.' for glob_filename.  */
  100. int noglob_dot_filenames = 1;
  101.  
  102. #ifdef MSDOS
  103. /* Nonzero if '\\' is to be treated as equivalent to '/' rather than as
  104.  * an escape character. */
  105. int backslash_same_as_slash = 1;
  106. #endif
  107.  
  108. static int glob_match_after_star ();
  109.  
  110. /* Return nonzero if PATTERN has any special globbing chars in it.  */
  111.  
  112. int
  113. glob_pattern_p (pattern)
  114.      char *pattern;
  115. {
  116.   register char *p = pattern;
  117.   register char c;
  118.  
  119.   while ((c = *p++) != '\0')
  120.     switch (c)
  121.       {
  122.       case '?':
  123.       case '[':
  124.       case '*':
  125.     return 1;
  126.  
  127.       case '\\':
  128. #ifdef MSDOS
  129.     if (backslash_same_as_slash)
  130.       break;
  131. #endif
  132.     if (*p++ == '\0')
  133.       return 0;
  134.       }
  135.  
  136.   return 0;
  137. }
  138.  
  139.  
  140. /* Match the pattern PATTERN against the string TEXT;
  141.    return 1 if it matches, 0 otherwise.
  142.  
  143.    A match means the entire string TEXT is used up in matching.
  144.  
  145.    In the pattern string, `*' matches any sequence of characters,
  146.    `?' matches any character, [SET] matches any character in the specified set,
  147.    [!SET] matches any character not in the specified set.
  148.  
  149.    A set is composed of characters or ranges; a range looks like
  150.    character hyphen character (as in 0-9 or A-Z).
  151.    [0-9a-zA-Z_] is the set of characters allowed in C identifiers.
  152.    Any other character in the pattern must be matched exactly.
  153.  
  154.    To suppress the special syntactic significance of any of `[]*?!-\',
  155.    and match the character exactly, precede it with a `\'.
  156.  
  157.    If DOT_SPECIAL is nonzero,
  158.    `*' and `?' do not match `.' at the beginning of TEXT.  */
  159.  
  160. int
  161. glob_match (pattern, text, dot_special)
  162.      char *pattern, *text;
  163.      int dot_special;
  164. {
  165.   register char *p = pattern, *t = text;
  166.   register char c;
  167.  
  168.   while ((c = *p++) != '\0')
  169.     switch (c)
  170.       {
  171.       case '?':
  172.     if (*t == '\0' || (dot_special && t == text && *t == '.'))
  173.       return 0;
  174.     else
  175.       ++t;
  176.     break;
  177.  
  178.       case '\\':
  179. #ifdef MSDOS
  180.     if (backslash_same_as_slash && c != *t++)
  181.       return 0;
  182. #endif
  183.     if (*p++ != *t++)
  184.       return 0;
  185.     break;
  186.  
  187.       case '*':
  188.     if (dot_special && t == text && *t == '.')
  189.       return 0;
  190.     return glob_match_after_star (p, t);
  191.  
  192.       case '[':
  193.     {
  194.       register char c1 = *t++;
  195.       int invert;
  196.  
  197.       if (c1 == '\0')
  198.         return 0;
  199.  
  200.       invert = (*p == '!');
  201.  
  202.       if (invert)
  203.         p++;
  204.  
  205.       c = *p++;
  206.       while (1)
  207.         {
  208.           register char cstart = c, cend = c;
  209.  
  210. #ifndef MSDOS
  211.           if (c == '\\')
  212.         {
  213.           cstart = *p++;
  214.           cend = cstart;
  215.         }
  216. #else
  217.           if (c == '\\' && !backslash_same_as_slash)
  218.         {
  219.           cstart = *p++;
  220.           cend = cstart;
  221.         }
  222. #endif
  223.  
  224.  
  225.           if (cstart == '\0')
  226.         return 0;    /* Missing ']'. */
  227.  
  228.           c = *p++;
  229.  
  230.           if (c == '-')
  231.         {
  232.           cend = *p++;
  233. #ifndef MSDOS
  234.           if (cend == '\\')
  235.             cend = *p++;
  236. #else
  237.           if (cend == '\\' && !backslash_same_as_slash)
  238.             cend = *p++;
  239. #endif
  240.           if (cend == '\0')
  241.             return 0;
  242.           c = *p++;
  243.         }
  244.           if (c1 >= cstart && c1 <= cend)
  245.         goto match;
  246.           if (c == ']')
  247.         break;
  248.         }
  249.       if (!invert)
  250.         return 0;
  251.       break;
  252.  
  253.     match:
  254.       /* Skip the rest of the [...] construct that already matched.  */
  255.       while (c != ']')
  256.         {
  257.           if (c == '\0')
  258.         return 0;
  259.           c = *p++;
  260.           if (c == '\0')
  261.         return 0;
  262. #ifndef MSDOS
  263.           if (c == '\\')
  264.         p++;
  265. #else
  266.           if (c == '\\' && !backslash_same_as_slash)
  267.         p++;
  268. #endif
  269.         }
  270.       if (invert)
  271.         return 0;
  272.       break;
  273.     }
  274.  
  275.       default:
  276.     if (c != *t++)
  277.       return 0;
  278.       }
  279.  
  280.   return *t == '\0';
  281. }
  282.  
  283. /* Like glob_match, but match PATTERN against any final segment of TEXT.  */
  284.  
  285. static int
  286. glob_match_after_star (pattern, text)
  287.      char *pattern, *text;
  288. {
  289.   register char *p = pattern, *t = text;
  290.   register char c, c1;
  291.  
  292.   while ((c = *p++) == '?' || c == '*')
  293.     if (c == '?' && *t++ == '\0')
  294.       return 0;
  295.  
  296.   if (c == '\0')
  297.     return 1;
  298.  
  299. #ifndef MSDOS
  300.   if (c == '\\')
  301.     c1 = *p;
  302.   else
  303.     c1 = c;
  304. #else
  305.   if (c == '\\' && !backslash_same_as_slash)
  306.     c1 = *p;
  307.   else
  308.     c1 = c;
  309. #endif
  310.  
  311.   --p;
  312.   while (1)
  313.     {
  314.       if ((c == '[' || *t == c1) && glob_match (p, t, 0))
  315.     return 1;
  316.       if (*t++ == '\0')
  317.     return 0;
  318.     }
  319. }
  320.  
  321. /* Return a vector of names of files in directory DIR
  322.    whose names match glob pattern PAT.
  323.    The names are not in any particular order.
  324.    Wildcards at the beginning of PAT do not match an initial period
  325.    if noglob_dot_filenames is nonzero.
  326.  
  327.    The vector is terminated by an element that is a null pointer.
  328.  
  329.    To free the space allocated, first free the vector's elements,
  330.    then free the vector.
  331.  
  332.    Return NULL if cannot get enough memory to hold the pointer
  333.    and the names.
  334.  
  335.    Return -1 if cannot access directory DIR.
  336.    Look in errno for more information.  */
  337.  
  338. char **
  339. glob_vector (pat, dir)
  340.      char *pat;
  341.      char *dir;
  342. {
  343.   struct globval
  344.   {
  345.     struct globval *next;
  346.     char *name;
  347.   };
  348.  
  349.   DIR *d;
  350.   register struct direct *dp;
  351.   struct globval *lastlink;
  352.   register struct globval *nextlink;
  353.   register char *nextname;
  354.   unsigned int count;
  355.   int lose;
  356.   register char **name_vector;
  357.   register unsigned int i;
  358. #ifdef MSDOS
  359.   char *allocated_dir = NULL;
  360. #endif
  361.  
  362. #ifdef MSDOS
  363.   /* We have to get rid of trailing slashes, but only if this isn't
  364.    * a root directory */
  365.   count = strlen (dir);
  366.   if (backslash_same_as_slash)
  367.     i = count > 1 && (dir[count - 1] == '/' || dir[count - 1] == '\\');
  368.   else
  369.     i = count > 1 && dir[count - 1] == '/';
  370.   if (i && dir[count - 2] != ':')
  371.     {
  372.       allocated_dir = malloc (count);
  373.       if (allocated_dir == NULL)
  374.     return NULL;
  375.       bcopy (dir, allocated_dir, count);
  376.       allocated_dir[count - 1] = '\0';
  377.       dir = allocated_dir;
  378.     }
  379. #endif
  380.  
  381.   d = opendir (dir);
  382.   if (d == NULL)
  383.     {
  384. #ifdef MSDOS
  385.       free (allocated_dir);
  386. #endif
  387.       return (char **) -1;
  388.     }
  389.  
  390.   lastlink = NULL;
  391.   count = 0;
  392.   lose = 0;
  393.  
  394.   /* Scan the directory, finding all names that match.
  395.      For each name that matches, allocate a struct globval
  396.      on the stack and store the name in it.
  397.      Chain those structs together; lastlink is the front of the chain.  */
  398.   while (1)
  399.     {
  400.       dp = readdir (d);
  401.       if (dp == NULL)
  402.     break;
  403. #ifndef MSDOS
  404.       if (dp->d_ino != 0
  405.       && glob_match (pat, dp->d_name, noglob_dot_filenames))
  406. #else
  407.       if (glob_match (pat, dp->d_name, noglob_dot_filenames))
  408. #endif
  409.     {
  410. #ifndef MSDOS
  411.       nextlink = (struct globval *) alloca (sizeof (struct globval));
  412. #else
  413.       /* Under MSDOS our stack is small--use malloc instead of alloca */
  414.       nextlink = (struct globval *) malloc (sizeof (struct globval));
  415.       if (nextlink == NULL)
  416.         {
  417.           lose = 1;
  418.           break;
  419.         }
  420. #endif
  421.       nextlink->next = lastlink;
  422.       i = D_NAMLEN (dp) + 1;
  423.       nextname = (char *) malloc (i);
  424.       if (nextname == NULL)
  425.         {
  426.           lose = 1;
  427.           break;
  428.         }
  429.       lastlink = nextlink;
  430.       nextlink->name = nextname;
  431.       bcopy (dp->d_name, nextname, i);
  432.       count++;
  433.     }
  434.     }
  435.   closedir (d);
  436.  
  437.   if (!lose)
  438.     {
  439.       name_vector = (char **) malloc ((count + 1) * sizeof (char *));
  440.       lose |= name_vector == NULL;
  441.     }
  442.  
  443.   /* Have we run out of memory?  */
  444.   if (lose)
  445.     {
  446.       /* Here free the strings we have got.  */
  447.       while (lastlink)
  448.     {
  449. #ifndef MSDOS
  450.       free (lastlink->name);
  451.       lastlink = lastlink->next;
  452. #else
  453.       /* We used malloc to allocate so we have to free by hand */
  454.  
  455.       void *temp;
  456.  
  457.       free (lastlink->name);
  458.       temp = lastlink;
  459.       lastlink = lastlink->next;
  460.       free (temp);
  461. #endif
  462.     }
  463. #ifdef MSDOS
  464.       free (allocated_dir);
  465. #endif
  466.       return NULL;
  467.     }
  468.  
  469.   /* Copy the name pointers from the linked list into the vector.  */
  470.   for (i = 0; i < count; ++i)
  471.     {
  472. #ifndef MSDOS
  473.       name_vector[i] = lastlink->name;
  474.       lastlink = lastlink->next;
  475. #else
  476.       /* We used malloc to allocate so we have to free by hand */
  477.  
  478.       void *temp;
  479.  
  480.       name_vector[i] = lastlink->name;
  481.       temp = lastlink;
  482.       lastlink = lastlink->next;
  483.       free (temp);
  484. #endif
  485.     }
  486.  
  487. #ifdef MSDOS
  488.   free (allocated_dir);
  489. #endif
  490.  
  491.   name_vector[count] = NULL;
  492.   return name_vector;
  493. }
  494.  
  495. /* Return a new array, replacing ARRAY, which is the concatenation
  496.    of each string in ARRAY to DIR.
  497.    Return NULL if out of memory.  */
  498.  
  499. static char **
  500. glob_dir_to_array (dir, array)
  501.      char *dir, **array;
  502. {
  503.   register unsigned int i, l;
  504.   int add_slash = 0;
  505.   char **result;
  506.  
  507.   l = strlen (dir);
  508.   if (l == 0)
  509.     return array;
  510.  
  511. #ifndef MSDOS
  512.   if (dir[l - 1] != '/')
  513.     add_slash++;
  514. #else
  515.   /* Treat `drive:' as if it were a slash terminated directory;
  516.    * if we were to stick on a slash then we'd be making a mistake--
  517.    * we want `no slash' to mean the current directory on the given drive. */
  518.   if (dir[l - 1] != '/' && dir[l - 1] != ':')
  519.     if (!backslash_same_as_slash || dir[l - 1] != '\\')
  520.       add_slash++;
  521. #endif
  522.  
  523.   for (i = 0; array[i] != NULL; i++)
  524.     ;
  525.  
  526.   result = (char **) malloc ((i + 1) * sizeof (char *));
  527.   if (result == NULL)
  528.     return NULL;
  529.  
  530.   for (i = 0; array[i] != NULL; i++)
  531.     {
  532.       result[i] = (char *) malloc (1 + l + add_slash + strlen (array[i]));
  533.       if (result[i] == NULL)
  534.     return NULL;
  535.       strcpy (result[i], dir);
  536.       if (add_slash)
  537.     result[i][l] = '/';
  538.       strcpy (result[i] + l + add_slash, array[i]);
  539.     }
  540.   result[i] = NULL;
  541.  
  542.   /* Free the input array.  */
  543.   for (i = 0; array[i] != NULL; i++)
  544.     free (array[i]);
  545.   free ((char *) array);
  546.   return result;
  547. }
  548.  
  549. /* Do globbing on PATHNAME.  Return an array of pathnames that match,
  550.    marking the end of the array with a null-pointer as an element.
  551.    If no pathnames match, then the array is empty (first element is null).
  552.    If there isn't enough memory, then return NULL.
  553.    If a file system error occurs, return -1; `errno' has the error code.
  554.  
  555.    Wildcards at the beginning of PAT, or following a slash,
  556.    do not match an initial period if noglob_dot_filenames is nonzero.  */
  557.  
  558. char **
  559. glob_filename (pathname)
  560.      char *pathname;
  561. {
  562.   char **result;
  563.   unsigned int result_size;
  564.   char *directory_name, *filename;
  565.   unsigned int directory_len;
  566. #ifdef MSDOS
  567.   char *string_to_append_to_directory = "";
  568. #endif
  569.  
  570. #ifdef TEST_MESSAGES
  571.   printf("pathname = %s\n",pathname);
  572. #endif
  573.   result = (char **) malloc (sizeof (char *));
  574.   result_size = 1;
  575.   if (result == NULL)
  576.     return NULL;
  577.  
  578.   result[0] = NULL;
  579.  
  580.   /* Find the filename.  */
  581.   filename = rindex (pathname, '/');
  582. #ifdef MSDOS
  583.   if (backslash_same_as_slash)
  584.     {
  585.       char *fname;
  586.  
  587.       if (filename == NULL)
  588.     fname = rindex (pathname, '\\');
  589.       else
  590.     fname = rindex (filename + 1, '\\');
  591.       if (fname != NULL)
  592.     filename = fname;
  593.     }
  594.   if (filename == NULL && isalpha (pathname[0]) && pathname[1] == ':')
  595.     filename = pathname + 1;
  596. #endif
  597.   if (filename == NULL)
  598.     {
  599.       filename = pathname;
  600.       directory_name = "";
  601.       directory_len = 0;
  602.     }
  603.   else
  604.     {
  605.       directory_len = (filename - pathname) + 1;
  606.  
  607. #ifdef MSDOS
  608.       if (filename == pathname + 1 && *filename == ':')
  609.     {
  610.       /* The files are supposed to be in the current directory on the
  611.        * specified drive.  This creates minor problems.  No matter.
  612.        * If the current directory is root, append a '/'.  If the
  613.        * current directory is not root, append a './'.  If the
  614.        * drive is invalid, then no match is possible and so set
  615.        * errno to ENOENT and return -1 */
  616.  
  617.       static char working_dir[_MAX_DIR];
  618.       char *get_working_directory ();
  619.  
  620.       if (!get_working_directory (working_dir,
  621.                        tolower (pathname[0]) - 'a' + 1))
  622.         {
  623.           errno = ENOENT;
  624.           return (char **) -1;
  625.         }
  626.  
  627.           string_to_append_to_directory =
  628.         (working_dir[0] == '\0') ? "/" : "./";
  629.     }
  630. #endif
  631.  
  632. #ifndef MSDOS
  633.       directory_name = (char *) alloca (directory_len + 1);
  634. #else
  635.       /* Under MSDOS, stack space is limited, so use malloc instead of
  636.        * alloca */
  637.       directory_name =
  638.           (char *) malloc (directory_len + 1 +
  639.                             strlen (string_to_append_to_directory));
  640.       if (directory_name == NULL)
  641.     return NULL;
  642. #endif
  643.  
  644.       bcopy (pathname, directory_name, directory_len);
  645.       directory_name[directory_len] = '\0';
  646. #ifdef MSDOS
  647.       /* Append `/' or `./' if necessary */
  648.       strcat (directory_name, string_to_append_to_directory);
  649.       directory_len += strlen (string_to_append_to_directory);
  650. #endif
  651.       ++filename;
  652.     }
  653.  
  654. #ifdef TEST_MESSAGES
  655.   printf("  directory_name = %s\n",directory_name);
  656.   printf("  filename       = %s\n",filename);
  657. #endif
  658.  
  659.   /* If directory_name contains globbing characters, then we
  660.      have to expand the previous levels.  Just recurse. */
  661.   if (glob_pattern_p (directory_name))
  662.     {
  663.       char **directories;
  664.       register unsigned int i;
  665.  
  666.       if (directory_name[directory_len - 1] == '/')
  667.     directory_name[directory_len - 1] = '\0';
  668. #ifdef MSDOS
  669.       else if (backslash_same_as_slash
  670.                 && directory_name[directory_len - 1] == '\\')
  671.     directory_name[directory_len - 1] = '\0';
  672. #endif
  673.  
  674.       directories = glob_filename (directory_name);
  675.       if (directories == NULL)
  676.     goto memory_error;
  677.       else if (directories == (char **) -1)
  678.     {
  679. #ifdef MSDOS
  680.       free ((void *) directory_name);
  681. #endif
  682.           return (char **) -1;
  683.     }
  684.       else if (*directories == NULL)
  685.     {
  686.       free ((char *) directories);
  687. #ifdef MSDOS
  688.       free ((void *) directory_name);
  689. #endif
  690.       return (char **) -1;
  691.     }
  692.  
  693.       /* We have successfully globbed the preceding directory name.
  694.      For each name in DIRECTORIES, call glob_vector on it and
  695.      FILENAME.  Concatenate the results together.  */
  696.       for (i = 0; directories[i] != NULL; i++)
  697.     {
  698.       char **temp_results = glob_vector (filename, directories[i]);
  699.       if (temp_results == NULL)
  700.         goto memory_error;
  701.       else if (temp_results == (char **) -1)
  702.         /* This filename is probably not a directory.  Ignore it.  */
  703.         ;
  704.       else
  705.         {
  706.           char **array = glob_dir_to_array (directories[i], temp_results);
  707.           register unsigned int l;
  708.  
  709.           l = 0;
  710.           while (array[l] != NULL)
  711.         ++l;
  712.  
  713.           result = (char **) realloc (result,
  714.                       (result_size + l) * sizeof (char *));
  715.           if (result == NULL)
  716.         goto memory_error;
  717.  
  718.           for (l = 0; array[l] != NULL; ++l)
  719.         result[result_size++ - 1] = array[l];
  720.           result[result_size - 1] = NULL;
  721.           free ((char *) array);
  722.         }
  723.     }
  724.       /* Free the directories.  */
  725.       for (i = 0; directories[i] != NULL; i++)
  726.     free (directories[i]);
  727.       free ((char *) directories);
  728.  
  729. #ifdef MSDOS
  730.       free ((void *) directory_name);
  731. #endif
  732.       return result;
  733.     }
  734.  
  735.   /* If there is only a directory name, return it. */
  736.   if (*filename == '\0')
  737.     {
  738.       result = (char **) realloc ((char *) result, 2 * sizeof (char *));
  739.       if (result == NULL)
  740.     {
  741. #ifdef MSDOS
  742.       free ((void *) directory_name);
  743. #endif
  744.       return NULL;
  745.     }
  746.       result[0] = (char *) malloc (directory_len + 1);
  747.       if (result[0] == NULL)
  748.     goto memory_error;
  749.       bcopy (directory_name, result[0], directory_len + 1);
  750.       result[1] = NULL;
  751. #ifdef MSDOS
  752.       free ((void *) directory_name);
  753. #endif
  754.       return result;
  755.     }
  756.   else
  757.     {
  758.       /* Otherwise, just return what glob_vector
  759.      returns appended to the directory name. */
  760.       char **temp_results = glob_vector (filename,
  761.                      (directory_len == 0
  762.                       ? "." : directory_name));
  763.  
  764.       if (temp_results == NULL || temp_results == (char **) -1)
  765.     {
  766. #ifdef MSDOS
  767.       free ((void *) directory_name);
  768. #endif
  769.       return temp_results;
  770.     }
  771.  
  772. #ifndef MSDOS
  773.       return glob_dir_to_array (directory_name, temp_results);
  774. #else
  775.       temp_results = glob_dir_to_array (directory_name, temp_results);
  776.       free ((void *) directory_name);
  777.       return temp_results;
  778. #endif
  779.     }
  780.  
  781. memory_error:;
  782. #ifdef MSDOS
  783.   free ((void *) directory_name);
  784. #endif
  785.   if (result != NULL)
  786.     {
  787.       register unsigned int i;
  788.       for (i = 0; result[i] != NULL; ++i)
  789.     free (result[i]);
  790.       free ((char *) result);
  791.     }
  792.   return NULL;
  793. }
  794.  
  795. #ifdef TEST
  796.  
  797. main (argc, argv)
  798.      int argc;
  799.      char **argv;
  800. {
  801.   char **value;
  802.   int i, optind;
  803.  
  804.   optind = 1;
  805.  
  806. #ifdef MSDOS
  807.   if (optind < argc)
  808.     {
  809.       if (strcmp (argv[optind], "0") == 0)
  810.     {
  811.       ++optind;
  812.       backslash_same_as_slash = 0;
  813.       printf ("backslash_same_as_slash = 0\n");
  814.     }
  815.       else if (strcmp (argv[optind], "1") == 0)
  816.     {
  817.       ++optind;
  818.       backslash_same_as_slash = 1;
  819.       printf ("backslash_same_as_slash = 1\n");
  820.     }
  821.     }
  822. #endif
  823.  
  824.   for (; optind < argc; optind++)
  825.     {
  826.       value = glob_filename (argv[optind]);
  827.       if (value == NULL)
  828.     puts ("virtual memory exhausted");
  829.       else if (value == (char **) -1)
  830.     perror (argv[optind]);
  831.       else
  832.     for (i = 0; value[i] != NULL; i++)
  833.       puts (value[i]);
  834.     }
  835.   exit (0);
  836. }
  837.  
  838. #endif /* TEST */
  839.